package com.example.demon.controllers;

import com.example.demon.service.AdminService;
import com.example.demon.entity.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class jwtController {

    @Autowired
    AdminService adminService;

    @Autowired
    PasswordEncoder passwordEncoder;

    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @RequestMapping("/admin")
    public String admin() {
        return "Admin";
    }

    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @RequestMapping("/client")
    public String client() {
        return "Client";
    }

    @RequestMapping("/guest")
    public String guest() {
        Admin admin = Admin.builder().username("admin").password(this.passwordEncoder.encode("123456")).build();
        adminService.saveAdmin(admin);
        return "Guest";
    }
}